/*
Copyright (c) 2006 - 2008  Universitaet Osnabrueck, virtUOS
Authors: Markus Ketterl, Johannes Emden, Nils Schuhmacher, Andreas Rickling

This file is part of virtPresenter.

virtPresenter is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

virtPresenter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with virtPresenter.  If not, see <http://www.gnu.org/licenses/>.
*/

package virtPresenter.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import com.adobe.cairngorm.model.ModelLocator;
	
	import virtPresenter.event.ChangedLectureFolderEvent;
	import virtPresenter.event.LoadVideoXMLEvent;
	import virtPresenter.event.VideoPlayerTimeChangeEvent;
	import virtPresenter.model.QueryString;
	import virtPresenter.model.XMLParser;
	import virtPresenter.model.virtPresenterLocator;

	/**
	 * Commands handles a ChangedLectureFolderEvent and .	
	 */	
	public class ChangedLectureFolderCommand implements ICommand
	{	
		private var model : virtPresenterLocator = virtPresenterLocator.getInstance();	
				
		/**
		 * Execute method is invoked by the ChangedLectureFolderEvent. Sets lecture folder, contributor name
		 * and lecture name for the new lecture. Informs the presentation SWF of the changed lecture folder.
		 * Throws a LoadVideoXMLEvent.
		 * 
		 * @parameters event ChangedLectureFolderEvent
		 * 
		 * @see virtPresenter.event.ChangedLectureFolderEvent
		 */
		public function execute(event:CairngormEvent):void
		{	
			trace("ChangedLectureFolderCommand. Command invoked by ChangedLectureFolderEvent!");
			
			model.defaultView.videoPlayer.currentState="loadingState";
			model.defaultView.enabled=false;
			//Instantiate the xmlParser
			model.xmlParser=new XMLParser();
			if(model.defaultView.videoPlayer.conn!=null)
			{
				model.defaultView.videoPlayer.closeConnection();
			}   
			
			var lectureIndex:int = ChangedLectureFolderEvent(event).index;			
			
			var qs:QueryString = new QueryString();   				
   			
   			var externSource:String = qs.parameters.lecture
   			var source:String = model.lectures[lectureIndex].valueOf().source;
   			var lectureName:String = model.lectures[lectureIndex].valueOf().title;
   			var contributorName:String = model.lectures[lectureIndex].valueOf().contributor; 
   			var currentLectureIndex:int=lectureIndex;  		
   			
   			// studip anforderung um direkt über die url eine lecture anzusprechen
   			if(qs.parameters.lecture!=undefined && lectureIndex==0)
   				{
   					for(var i:int = 0; i<model.lectures.length; i++)
	   				{
	   					var tmp:String = model.lectures[i].valueOf().source;
	   					if(externSource==tmp)
	   					{
	   						source=externSource;
	   						lectureName= model.lectures[i].valueOf().title;
	   						contributorName= model.lectures[i].valueOf().contributor;
	   						currentLectureIndex=i;
	   						model.defaultView.userAccordion.lectureList.selectedIndex=i;
	   						break;
	   					}
	   				}	   				
	  
   				}  			   			   				
												
			model.LectureName=lectureName;
			model.lectureFolder=source;
			model.dozent=contributorName;
			model.currentLectureIndex=currentLectureIndex;
			
			//Set default values
			model.currentSlideID=1;
			model.currentvideotime=0;
			model.currentAnimStep=0;
			
			
			model.selectedColor=0xa2abbe;  
			model.selectedThumbnailIndex=0;				
						
			try { 
				model.mainWindowProxy.requestLecture(model.lectureFolder);
				model.previewLeftProxy.requestLecture(model.lectureFolder);
				model.previewMiddleProxy.requestLecture(model.lectureFolder);
				model.previewRightProxy.requestLecture(model.lectureFolder);				     			
			}
			catch(error:ArgumentError) {
				trace ("Error in sendingLC");
			}
			
			CairngormEventDispatcher.getInstance().dispatchEvent(new LoadVideoXMLEvent(model.lectureFolder));
			
		
				
		}		
		
	}
}